package com.xuan.bigappleui.lib.utils.ui;

import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;

/**
 * 根据名称来获取资源的resid，这样就可以绕开R文件来获取系统的资源，就可以打包成Jar供第三方调用
 * 
 * @author xuan
 * @version $Revision: 1.0 $, $Date: 2013-9-26 下午3:59:14 $
 */
public class M {
	public static int styleable(Context context, String name) {
		return getResidByName(context, "styleable", name);
	}

	public static int layout(Context context, String name) {
		return getResidByName(context, "layout", name);
	}

	public static int dimen(Context context, String name) {
		return getResidByName(context, "dimen", name);
	}

	public static int drawable(Context context, String name) {
		return getResidByName(context, "drawable", name);
	}

	public static int string(Context context, String name) {
		return getResidByName(context, "string", name);
	}

	public static int style(Context context, String name) {
		return getResidByName(context, "style", name);
	}

	public static int id(Context context, String name) {
		return getResidByName(context, "id", name);
	}

	/**
	 * 获取资源resid
	 * 
	 * @param context
	 *            上下文
	 * @param className
	 *            R文件中内部类名字，例如：drawable，即资源的分类
	 * @param name
	 *            资源名称
	 * @return
	 */
	public static int getResidByName(Context context, String className,
			String name) {
		String residKey = getResidKey(className, name);
		Integer residInteger = cacheResId.get(residKey);
		if (null != residInteger) {
			return residInteger;
		} else {
			String packageName = context.getPackageName();
			Class<?> r = null;
			int resid = 0;
			try {
				r = Class.forName(packageName + ".R");

				Class<?>[] classes = r.getClasses();
				Class<?> desireClass = null;// 想要找到的className类

				for (int i = 0; i < classes.length; ++i) {
					if (classes[i].getName().split("\\$")[1].equals(className)) {
						desireClass = classes[i];
						break;
					}
				}

				if (desireClass != null) {
					resid = desireClass.getField(name).getInt(desireClass);
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				e.printStackTrace();
			}

			cacheResId.put(residKey, resid);
			return resid;
		}
	}

	/**
	 * 获取资源resid数组
	 * 
	 * @param context
	 *            上下文
	 * @param className
	 *            R文件中内部类名字，例如：drawable
	 * @param name
	 *            资源名
	 * @return
	 */
	public static int[] getIdsByName(Context context, String className,
			String name) {
		String packageName = context.getPackageName();
		Class<?> r = null;
		int[] resids = null;
		try {
			r = Class.forName(packageName + ".R");

			Class<?>[] classes = r.getClasses();
			Class<?> desireClass = null;

			for (int i = 0; i < classes.length; ++i) {
				if (classes[i].getName().split("\\$")[1].equals(className)) {
					desireClass = classes[i];
					break;
				}
			}

			if ((desireClass != null)
					&& (desireClass.getField(name).get(desireClass) != null)
					&& (desireClass.getField(name).get(desireClass).getClass()
							.isArray())) {
				resids = (int[]) desireClass.getField(name).get(desireClass);
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}

		return resids;
	}

	// ///////////////////////////////////////////////缓存部分//////////////////////////////////////////////////////////
	/**
	 * 用来缓存去过的resid，这样第二次取相同的resid就不再用反射去取了
	 */
	private static ConcurrentHashMap<String, Integer> cacheResId = new ConcurrentHashMap<String, Integer>();

	/**
	 * 清理缓存
	 */
	public void clearCacheResId() {
		cacheResId.clear();
	}

	private static String getResidKey(String className, String name) {
		return className + "-" + name;
	}

}
